//
//  ScrollableLabelView.swift
//  SCA
//
//  Created by tjun on 2025/9/4.
//

import UIKit

class ScrollableLabelView: UIView {
    
    private let iconView = UIImageView()
    private let arrowImageView = UIImageView(image: UIImage(named: "small_arrow"))
    private let scrollView = UIScrollView()
    private let textLabel = UILabel()
    
    var text: String? {
        didSet {
            textLabel.text = text
            layoutIfNeeded()
        }
    }
    
    var textColor: UIColor {
        get { textLabel.textColor }
        set { textLabel.textColor = newValue }
    }
    
    var font: UIFont {
        get { textLabel.font }
        set { textLabel.font = newValue }
    }
    
    var icon: UIImage? {
        get { iconView.image }
        set { iconView.image = newValue }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        
        addSubview(arrowImageView)
        
        scrollView.showsHorizontalScrollIndicator = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.alwaysBounceHorizontal = true
        addSubview(scrollView)
        
        iconView.contentMode = .scaleAspectFit
        scrollView.addSubview(iconView)
        
        textLabel.textColor = .white
        textLabel.font = UIFont.systemFont(ofSize: 16)
        textLabel.numberOfLines = 1
        scrollView.addSubview(textLabel)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        arrowImageView.frame = CGRectMake(self.width - 20, (self.height - 10) * 0.5, 10, 10)
        scrollView.frame = CGRectMake(0, 0, self.width - 30, self.height)
        
        iconView.frame = CGRect(x: 12, y: (bounds.height - 22) / 2, width: 24, height: 22)
        
        textLabel.sizeToFit()
        textLabel.frame.origin = CGPoint(x: iconView.maxX + 8, y: (bounds.height - textLabel.frame.height) / 2)
        scrollView.contentSize = CGSize(width: textLabel.frame.width + iconView.maxX + 16, height: bounds.height)
    }
}
